<template>
  <view class="container">
    <!-- 使用 uni-list 组件 -->
    <uni-list>
      <uni-list-item 
        v-for="(item,index) in list" 
        :key="index"
        :title="item.title"
        :note="item.createTime"
        clickable
		@longpress="handleItemLongpress(index)"
        @click="handleItemClick(item.id)"
      >
        <!-- 右侧内容插槽 -->
        <template v-slot:footer>
          <view :class="['rate', getRateClass(item.fitness)]">
            {{ (item.fitness || 0) }}%
          </view>
        </template>
      </uni-list-item>
    </uni-list>
	<uni-popup ref="alertDialog" type="dialog">
		<uni-popup-dialog type="info" cancelText="关闭" confirmText="同意" title="删除" content="确定删除当前信息吗？" @confirm="dialogConfirm"
			@close="dialogClose"></uni-popup-dialog>
	</uni-popup>
  </view>
</template>

<script>
import { getToken } from '@/utils/auth'
import { listOptimizationRecordBycreateBy,deleteOptimizationById } from "@/api/optimization.js"
export default {
  data() {
    return {
      list: [],
	  deleteIndex:null
    }
  },
  onShow () {
    if (!getToken()) {
		uni.reLaunch({ url: "/pages/login" })
		return false
	}
	this.loadData();
  },
  onShareTimeline() {
    return {
      title: '免费计算最优下料方案',
      path: '/pages/index/index', // 分享后跳转的页面路径
      imageUrl: '/static/share.jpg', // 可选，分享图的地址
  	type:'0'
    };
  },
  onShareAppMessage()  {
    return {
      title: '免费计算最优下料方案',
      path: '/pages/index/index', // 分享后跳转的页面路径
      imageUrl: '/static/share.jpg', // 可选，分享图的地址
  	type:'0'
    };
  },
  methods: {
	loadData(){
		listOptimizationRecordBycreateBy().then(res=>{
			if(res.code === 200){
				this.list = res.rows;
			}
		})
	},
    // 处理点击跳转
    handleItemClick(id) {
      uni.navigateTo({
        url: `/pages/history/detail/detail?id=${id}`
      })
      
    },
	handleItemLongpress(index){
		this.deleteIndex = index;
		this.$refs.alertDialog.open()
	},
	// 删除
	dialogConfirm() {
		console.log('点击确认')
		deleteOptimizationById(this.list[this.deleteIndex].id).then(res=>{
			if(res.code == 200){
				uni.showToast({
					icon:'success',
					title:"已删除"
				})
				this.list.splice(this.deleteIndex,1);
				
			}
		})
	},
	dialogClose() {
		this.$data.deleteIndex=null;
		console.log('点击关闭')
	},
    // 获取优化率颜色类名
    getRateClass(rate) {
      return rate > 0 ? 'positive' : rate < 0 ? 'negative' : 'neutral'
    }
  }
}
</script>

<style lang="scss">

.container {
  padding: 20rpx;
  background-color: #f8f8f8;
}

// 优化率样式
.rate {
  font-size: 28rpx;
  font-weight: bold;
  
  &.positive {
    color: #09bb07;
  }
  
  &.negative {
    color: #e64340;
  }
  
  &.neutral {
    color: #888;
  }
}

// 调整列表项间距
.uni-list-item__container {
  margin-bottom: 10rpx;
}
</style>
